package com.zjuh.qss.biz.ao.impl;

import com.zjuh.qss.biz.ao.TagsWorkerAO;

public class TagsWorkerAOImpl extends BaseAO implements TagsWorkerAO {
/*
	private InputTagDAO inputTagDAO;
	
	private SimilarTagDAO similarTagDAO;
	
	private LinkFetcher linkFetcher = new BaiduLinkFetcher();
	
	public void runFetchTags() {
		try {
			log.warn("start fetch tags...");
			int page = 1;
			int count = 0;
			while (true) {
				InputTagQuery inputTagQuery = new InputTagQuery();
				inputTagQuery.setPageNo(page);
				inputTagQuery.setPageSize(100);
				List<InputTagDO> inputTags = inputTagDAO.queryForPage(inputTagQuery);
				if (CollectionUtil.isEmpty(inputTags)) {
					break;
				}
				for (InputTagDO inputTag : inputTags) {
					processTag(inputTag);
					++count;
				}
				++page;
			}
			
			log.warn("fetch tag finish. count: " + count);
		} catch (Exception e) {
			log.error("runFetchTags error", e);
		}
	}
	
	private void processTag(InputTagDO inputTag) throws IOException {
		String name = inputTag.getName();
		Set<String> links = linkFetcher.fetchLinks(name);
		StringBuilder sb = new StringBuilder(1024);
		for (String link : links) {
			String content = fetchLinkContent(link);
			sb.append(content);
		}
		SimilarTagDO similarTagDO = new SimilarTagDO();
		similarTagDO.setIntputText(sb.toString());
		similarTagDO.setName(name);
		similarTagDAO.createIfNotExist(similarTagDO);
		log.warn("fetch tag: " + inputTag.getName() + " finish.");
	}
	
	private String fetchLinkContent(String url) throws IOException {
		try {
			ClientConnector clientConnector = new JDKClientConnector();
			ClientResponse clientResponse = clientConnector.request(url, false, null, null, null, null);
			byte[] bytes = clientResponse.getData();
			Document doc = Jsoup.parse(new FastByteArrayInputStream(bytes), null, "/");
			String content = doc.text();
			StringBuilder sb = new StringBuilder(content.length());
			for (int len = content.length(), i = 0; i < len; ++i) {
				char c = content.charAt(i);
				if (isValidChar(c)) {
					sb.append(c);
				}
			}
			return sb.toString();
		} catch (Exception e) {
			log.error("fetchLinkContent failed", e);
			return null;
		}
	}
	
	static boolean isValidChar(char c) {
		if (c >= '0' && c <= '9') {
			return true;
		}
		if (c >= 'a' && c <='z' ) {
			return true;
		}
		if (c >= 'A' && c <= 'Z') {
			return true;
		}
		if (c == ' ' || c == '\t' || c == '\n') {
			return true;
		}
		if (c >= '\u4e00' && c <= '\u9fa5') {
			// 汉字
			return true;
		}
		return false;
	}
	
	static abstract class LinkFetcher {
		protected abstract String getUrl(String name);
		protected abstract String getCharset();
		protected abstract Set<String> getIngoreHosts();
		
		public Set<String> fetchLinks(String name) throws IOException {
			String url = getUrl(name);
			ClientConnector clientConnector = new JDKClientConnector();
			ClientResponse clientResponse = clientConnector.request(url, false, null, null, null, null);
			byte[] bytes = clientResponse.getData();
			String charset = JDKClientConnector.getCharset(clientResponse.getHeaders());
			if (StringUtil.isEmpty(charset)) {
				charset = getCharset();
			}
			String content = new String(bytes, charset);
			List<String> links = parseUrls(content);
			Set<String> ret = new HashSet<String>(links.size());
			Set<String> ignoreHosts = getIngoreHosts();
			if (ignoreHosts == null) {
				ignoreHosts = new HashSet<String>(0);
			}
			for (String link : links) {
				try {
					URL x = new URL(link);
					String host = x.getHost();
					if (!ignoreHosts.contains(host)) {
						String targetLink = StringUtil.getFirstBefore(link, "#");
						ret.add(targetLink);
					}
				} catch (Exception e) {
					continue;
				}
			}
				
			return ret;
		}
		
		protected List<String> parseUrls(String content) {
			return HtmlContentParser.parseForLinks(content);
		}
	}
	
	static class BaiduLinkFetcher extends LinkFetcher {

		private Set<String> ignoreHosts = CollectionUtil.newHashSet(Arrays.asList(
				"map.baidu.com", "e.baidu.com", "passport.baidu.com", "mp3.baidu.com",
				"image.baidu.com", "video.baidu.com", "open.baidu.com", "www.baidu.com",
				"cache.baidu.com", "share.baidu.com"));
		
		@Override
		protected Set<String> getIngoreHosts() {
			return ignoreHosts;
		}

		@Override
		protected String getCharset() {
			return "gbk";
		}

		protected String getUrl(String name) {
			return "http://www.baidu.com/s?wd=" + name;
		}
		
	}
	
	public void setInputTagDAO(InputTagDAO inputTagDAO) {
		this.inputTagDAO = inputTagDAO;
	}

	public void setSimilarTagDAO(SimilarTagDAO similarTagDAO) {
		this.similarTagDAO = similarTagDAO;
	}
*/
}
